package com.kunbo.cn.service.impl.core.supplier;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.supplier.SupplierDataPermissionsDao;
import com.kunbo.cn.entity.core.customer.CustomerDataPermissions;
import com.kunbo.cn.entity.core.supplier.SupplierDataPermissions;
import com.kunbo.cn.service.core.supplier.SupplierDataPermissionsService;

@Service
public class SupplierDataPermissionsImpl implements SupplierDataPermissionsService{

	@Autowired
	private SupplierDataPermissionsDao dao;
	@Autowired
	private JdbcTemplate jdbc;
	
	
	@Override
	public List<Map<String, Object>> selSupplier(String supplierName) {
		// TODO Auto-generated method stub
		String sql = "select a.type,a.id,a.create_time,c.`name` as `create_name`, b.`name` as type_name  from business_supplier_users_data_permissions a , business_supplier_manage b,db_system_user_user c  where 1= 1 and a.customerid = b.id and a.create_pople = c.id and a.userid = '" +supplierName+ "'";
		return jdbc.queryForList(sql);
	}

	@Override
	public Boolean delSupplier(String id) {
		// TODO Auto-generated method stub
		dao.delete(id);
		SupplierDataPermissions permissions = dao.findOne(id);
		if(permissions != null)
			return true;
		else
			return false;
	}


	@Override
	public SupplierDataPermissions saveSupplier(String userid, Integer customerid, Integer create_pople, String type) {
		// TODO Auto-generated method stub
		SupplierDataPermissions permissions = new SupplierDataPermissions();
		permissions.setUserid(userid);
		permissions.setCustomerid(customerid);
		permissions.setCreate_pople(create_pople);
		permissions.setType(type);
		SimpleDateFormat format = new SimpleDateFormat();
		permissions.setCreateTime(format.format(new Date()));
		dao.saveAndFlush(permissions);
		if(permissions != null)
			return permissions;
		else
			return null;
	}

}
